<template>
    <div class="home_box"> 
        <el-container class="home-container">
            <el-header >
                <div>
                    <img src="../../assets/heima.png" alt="">
                    <span>电商后台管理系统</span>
                    <el-button class="outbtn" type="success" @click="logout">退出</el-button>
                </div>
            </el-header>
            <el-container>
                <!-- 侧边栏 -->
                <el-aside :width="isCollapse ? '64px':'200px'">
                    <div class="toggle-button" @click="toogleButn">|||</div>
                    <el-menu 
                        background-color="#1486a0"
                        text-color="#fff"
                        active-text-color="#409EFF"  
                        :unique-opened="true" :collapse="isCollapse" 
                        :collapse-transition='false' router
                        :default-active="activePath">
                        <!-- 通过布尔值 绑定是否折叠；取消动画  开启路由模式-->
                        <!-- 一级菜单 数值和字符串拼接 就是一个字符串 -->
                        <el-submenu :index="item.id+''" v-for="(item) in menuList" :key="item.id"> 
                            <!-- 一级菜单模板区 -->
                            <template slot="title">
                                <!-- 图标 每一个对象的 id 就是对应的图标-->
                                <i :class="iconObj[item.id]" style="font-size:25px"></i>
                                <!-- 文本 -->
                                <span>{{item.authName}}</span>
                            </template> 
                            <!-- 二级菜单 -->
                            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children"
                             :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                                 <template slot="title">
                                    <i class="el-icon-menu"></i>
                                    <span>{{subItem.authName}}</span>
                                </template> 
                            </el-menu-item> 
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 右侧主体内容 -->
                <el-main>
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
export default {
    name:'Home',
    data(){
        return {
            menuList:[],
            // id 对应一个图标
            iconObj:{
                '125':'iconfont icon-yonghu',
                '103':'iconfont icon-quanxianguanli',
                '101':'iconfont icon-shangpinguanli',
                '102':'iconfont icon-dingdanguanli',
                '145':'iconfont icon-shujutongji'
            },
            // 是否折叠 
            isCollapse:false,
            // 被激活的地址
            activePath:'',

        }
    },
    created(){
        // 获取所有菜单
        this.getMenuList();
        // 当Home被创建的时候，路径被赋值
        this.activePath = window.sessionStorage.getItem("activePath")
    },
    methods:{
        // 退出功能
        logout(){
            window.sessionStorage.clear();
            this.$router.push('/login');
        },
        // 获取所有菜单数据
        async getMenuList(){
            const {data:result} = await this.$http.get('/menus');
            if(result.meta.status !== 200) return this.$message.error(result.meta.msg);
            // 保存数据
            this.menuList = result.data
            console.log(this.menuList,"菜单数据");
        },
        // 控制是否折叠
        toogleButn(){
            this.isCollapse = !this.isCollapse;
        },
        // 保存激活状态
        saveNavState(active){
            this.activePath = active;
            window.sessionStorage.setItem("activePath",active);
        }
    }
}
</script>

<style lang="less" scoped>
.home-container, .home_box {
    height: 100%;
}
.el-header {
    background-color: #2e95ce;
    display: flex;
    width: 100%;
    > div > span {
        vertical-align: middle;
        display: inline-block;
        margin-left: 25px;
        color: white;
    }
    > div > img {
        vertical-align: middle;
    }
    > div > .outbtn { 
        position: absolute;
        right:20px;
        top: 10px;
    }
}
.el-aside {
    background-color: #1486a0;
}
.el-main {
    background-color: #2AA9DD;
}
.iconfont{
    margin-right: 10px;
}
.el-menu {
    border-right: 0px;
}
.toggle-button{
    background-color: rgb(23, 178, 240);
    color: white;
    font-size: 18px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2rem;
    cursor: pointer;
}
</style>